package com.itheima;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.function.Consumer;

public class HashSetDemo {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();//不能去除重复的数据
        list.add("zq");
        list.add("yk");
        list.add("zx");
        list.add("zx");
        list.add("zx");
        list.add("zz");
        System.out.println(list);

        System.out.println(new HashSet<String>(list));//去除重复的数据,将list集合中的数据放入到HashSet集合中

        ArrayList<String> newlist = new ArrayList<String>(new HashSet<String>(list));//转换为ArrayList集合
        System.out.println(newlist);
        System.out.println("-------------------------------");
        //1. 创建HashSet对象
        HashSet<String> hash = new HashSet<String>(); //可以去除重复的数据，无序
        //2. 保存数据
        hash.add("zq");
        hash.add("yk");
        hash.add("zx");
        list.add("zx");
        list.add("zx");
        hash.add("zz");
        System.out.println(hash);
        //3. 用迭代器取数据
        // 注：for循环用不了，无get()方法,不能用for循环遍历
        Iterator<String> iterator = hash.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
        System.out.println("--------------------");
        //4. 取数据（增强for循环）
        for (String name : hash) {
            System.out.println(name);
        }

        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        for (int i : arr) {
            System.out.print(i+" ");
        }
        System.out.println("--------------------");
        //5.取数据（Lambda）
        hash.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println();
        hash.forEach((String s) -> {
                System.out.println(s);
        });
        System.out.println("--------------------");
        hash.forEach(name -> System.out.println(name));
        System.out.println("--------------------");
        hash.forEach(System.out::println);

        Student student = new Student("张", 18);
        System.out.println(student.toString());


    }


}
